<?php
/*
Plugin Name: Links Plus Widget
Plugin URI: http://tbicl.org/#
Description: A little help for my friends.
Author: D. Kretz
Version: 0.0.1
Author URI: http://localhost
*/

class Widget_Links_Plus extends WP_Widget {

	public function Widget_Links_Plus() {
		$widget_ops = array('description' => __( "Selected links" ) );
		$this->WP_Widget('links_plus', __('Links Plus'), $widget_ops);
	}

	public function widget( $args, $instance ) {
		extract($args, EXTR_SKIP);

        $title = isset($instance['title']) ? $instance['title'] : false;
		// $show_description = isset($instance['description']) ? $instance['description'] : false;
		$show_name = isset($instance['name']) ? $instance['name'] : false;
		// $show_rating = isset($instance['rating']) ? $instance['rating'] : false;
		// $show_images = isset($instance['images']) ? $instance['images'] : true;
		$category = isset($instance['category']) ? $instance['category'] : false;

		if ( is_admin() && !$category ) {
			// Display All Links widget as such in the widgets screen
			echo $before_widget . $before_title. __('All Links') . $after_title . $after_widget;
			return;
		}

		$before_widget = preg_replace('/id="[^"]*"/','id="%id"', $before_widget);
		wp_list_bookmarks(apply_filters('widget_links_plus_args', array(
            'title_li' => $title,
            'categorize' => 0,
			'title_before' => $before_title,
            'title_after' => $after_title,
			'category_before' => $before_widget,
            'category_after' => $after_widget,
			// 'show_images' => $show_images,
            // 'show_description' => $show_description,
			'show_name' => 0,
            // 'show_rating' => 0,
			'category' => $category,
            'class' => 'linkcat widget',
            'orderby' => 'rating',
            'order' => 'ASC'
		)));
	}

	public function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
		$instance['title'] = strip_tags($new_instance['title']);
		$instance['name'] = ! empty($new_instance['name']) ;
		// $instance['images'] = ! empty($new_instance['images']) ;
		// $instance['description'] = ! empty($new_instance['description']) ;
		// $instance['rating'] = ! empty($new_instance['rating']) ;
		$instance['category'] = empty($new_instance['category']) 
            ? false 
            : $new_instance['category'];
		return $instance;
	}

	public function form( $instance ) 
    {

		//Defaults
		$instance = wp_parse_args( (array) $instance, 
                                    array( 
                                        // 'images'      => true, 
                                           'name'        => true, 
                                        // 'description' => false, 
                                           // 'rating'      => false, 
                                           'title'       => false,
                                           'category'    => false ) );

		$link_cats      = get_terms( 'link_category');

        $titleid        = $this->get_field_id('title');
        $titlename      = $this->get_field_name("title");
        $titlecap       = __('Title');
        $titleval       = empty($instance['title']) 
                      ? "" 
                      : attribute_escape(stripslashes($instance['title'])); 

        $catid          = $this->get_field_id('category');
        $catname        = $this->get_field_name("category");
        $catcap         = __('Category');
        $catval         = empty($instance['category']) ? false : $instance['category'];

        echo   "<label for='{$titleid}'>{$titlecap}</label>
                <input type='text' name='${titlename}' id='{$titleid}'
                value='{$titleval}' />
		<p>
        ";

        echo   "
		<label for='{$catid}'>";
        _e('Link Category'); 
        echo "</label>
		<select class='widefat' id='{$catid}' name='{$catname}'>\n";

        foreach ( $link_cats as $link_cat ) {
            $id = $link_cat->term_id;
            $name = $link_cat->name;
            echo "<option value='{$id}'";
            if(intval($id) == intval($catval)) {
                echo " selected='selected'";
            }
            echo ">{$name}</option>\n";
        }
		echo "
		</select>
        </p>
        
        <p>\n";

        /*
        $this->_echo_check_box("images", "Show Link Image");

        echo "
		<label for='". $this->get_field_id('images') ."'>". _e('Show Link Image')."</label>
        <br />
        */
        echo "
		<input type='checkbox' class='checkbox' "; checked($instance['name'], true); echo "
            id='". $this->get_field_id('name') ."' 
            name='". $this->get_field_name('name') ."' />
        ";
        ?>
		<label for="<?php echo $this->get_field_id('name'); ?>"><?php _e('Show Link Name'); ?></label>
        <br />
        <?php
        /*
		<input class="checkbox" type="checkbox" <?php checked($instance['description'], true) ?> id="<?php echo $this->get_field_id('description'); ?>" name="<?php echo $this->get_field_name('description'); ?>" />
		<label for="<?php echo $this->get_field_id('description'); ?>"><?php _e('Show Link Description'); ?></label>
        <br />
        ?>
        <?php
/*
		<input class="checkbox" type="checkbox" <?php checked($instance['rating'], true) ?> id="<?php echo $this->get_field_id('rating'); ?>" name="<?php echo $this->get_field_name('rating'); ?>" />
		<label for="<?php echo $this->get_field_id('rating'); ?>"><?php _e('Show Link Rating'); ?></label>
		</p>
<?php
*/
	}

    private function _echo_check_box($optname, $prompt) {
        echo "<label for='".$this->get_field_id($optname)."'>".__($prompt)."</label><br />
        ";
        echo "<input type='checkbox' class='checkbox' ";
        checked($this->instance[$optname], true);
        echo " id='".$this->get_field_id($optname)."' ";
        echo " name='".$this->get_field_name($optname)."' />
        ";
    }
}

function widget_links_plus_init() {
	register_widget('Widget_Links_Plus');
}

add_action('init', 'widget_links_plus_init', 1);
?>
